// C1 E18 Edition3
// Add: Count how many ' ' and '\t'in the trail.

// DEBUG: must use while (s[i - 1] == ' ' || s[i - 1] == '\t'), but not in maine2.c.
// Add: Use functions.

#include <stdio.h>

#define MAXLINE 1000

int getstr(char s[], int lim);
void modifystr(char source[], int len);

int main()
{
    int len;
    char s[MAXLINE];

    len = 0;

    while ((len = getstr(s, MAXLINE)) > 0)
    {
        modifystr(s, len);
    }

    return 0;
}

void modifystr(char s[], int len)
{
    int i;
    int nb, nt;

    i = len;
    nb = nt = 0;

    if (s[i - 1] == '\n') // Case 1: have'\n'
    {
        printf("\n>>>Have '\\n' in the trail.\n");
        i--;
        while (s[i - 1] == ' ' || s[i - 1] == '\t')
        {
            if (s[i - 1] == ' ')
                nb++;
            else
                nt++;
            s[i - 1] = '\n', s[i] = '\0', i--;
        }
        i++;
        if (i == 1)
            printf(">>>Blank, delete.\n");
        else
            printf(">>>Source = %3d\t\tNow = %3d\tDelete ' ' %d\tDelete '\\t' %d\n>>>%s\n\n", len, i, nb, nt, s);
    }
    else // Case 2: not have'\n'
    {
        printf("\n>>>Not have '\\n' in the trail.\n");
        while (s[i - 1] == ' ' || s[i - 1] == '\t')
        {
            if (s[i - 1] == ' ')
                nb++;
            else
                nt++;
            s[i - 1] = '\0', i--;
        }
        if (i == 0)
            printf(">>>Blank, delete.\n");
        else
            printf(">>>Source = %3d\t\tNow = %3d\tDelete ' ' %d\tDelete '\\t' %d\n>>>%s\n\n", len, i, nb, nt, s);
    }
}

int getstr(char s[], int lim)
{
    int c;
    int i;

    i = 0;
    lim = MAXLINE;

    while (i < lim - 1 && (c = getchar()) != EOF && c != '\n')
    {
        s[i] = c;
        i++;
    }
    if (c == '\n')
        s[i] = '\n',
        i++;
    s[i] = '\0';

    return i;
}